//
//  ContentViewController.swift
//  DynamicColorDemo
//
//  Created by 李煜 on 2023/3/7.
//

import DynamicColor
import UIKit

class ContentViewController: UIViewController {
  var index: Int = 0
  let colors: [DynamicColor] = {
    //    1.  基本使用
    let color101 = UIColor(hexString: "#3498db")
    let color102 = UIColor(hex: 0x3498db)
    //    跨平台请使用DynamicColor
    let dynamic101 = DynamicColor(hexString: "#3498db")
    let dynamic102 = DynamicColor(hex: 0x3498db)
    //    2.Darken & Lighten

    let color200 = DynamicColor(hex: 0xc0392b)
    let color201 = color200.lighter()
    let color202 = color200.darkened()

    //    3.saturate & desaturate & Grayscale
    let originalColor3 = DynamicColor(hex: 0xc0392b)
    let saturatedColor = originalColor3.saturated()
    let desaturatedColor = originalColor3.desaturated()
    let grayscaledColor = originalColor3.grayscaled()

    //    反转
    let invertOriginal = originalColor3
    let invertedColor1 = invertOriginal.inverted()
    let invertedColor2 = UIColor.yellow.inverted()
    let invertedColor3 = invertedColor2.inverted()
    //    mix
    let mixedColor = originalColor3.mixed(withColor: .blue)
    let mixedGreen = originalColor3.mixed(withColor: .green)
    let mixedYellow = originalColor3.mixed(withColor: .yellow)

    let colors = [color101, color102, dynamic101, dynamic102,
                  color200, color201, color202, color200,
                  originalColor3, saturatedColor, desaturatedColor, grayscaledColor,
                  invertOriginal, invertedColor1, invertedColor2, invertedColor3,
                  mixedColor, mixedGreen, mixedYellow]
    return colors
  }()


  

  override func viewDidLoad() {
    super.viewDidLoad()
//Array
    var total = [DynamicColor]()
    total += colors

    setupViews(array: total)
  }

  func setupViews(array: [DynamicColor]) {
    let preferColumn = 4
    let width = 50
    let height = 50
    let margin = 20

    for i in 0 ... colors.count - 1 {
      let row = i / preferColumn
      let column = i % preferColumn
      let x = margin + (margin + width) * column
      let y = margin + (margin + height) * row

      let item = UIView(frame: .init(x: x, y: y, width: width, height: height))
      item.layer.cornerRadius = 25
      item.layer.masksToBounds = true
      item.backgroundColor = colors[i]
      view.addSubview(item)
    }
  }
}
